const app = getApp()

Page({
  data: {
    id:""
  },
  onLoad: function (options) {
    this.setData({
        id:options.id
    })
  },
  onTapPic: function () {
    this.authorizeCheck(0)
  },
  onTapTake: function () {
    this.authorizeCheck(1)
  },
  authorizeCheck: function (type) {
    var that = this
    tt.getSetting({
      success: (res) => {
        console.log("login" + JSON.stringify(res))
        if (JSON.stringify(res.authSetting) != "{}") {
          if (res.authSetting['scope.album'] == false || res.authSetting['scope.camera'] == false) {
            tt.showModal({
              title: '授权提醒',
              content: '未授权将不能使用此小程序相关功能，请授权相册和拍照功能',
              success: function (res) {
                tt.openSetting({
                  success: (res) => {
                    if (res.authSetting['scope.album'] || res.authSetting['scope.camera']) {
                      that.openAlbumAndCanera(type)
                    } else {
                      app.showToastMsg("调用失败")
                    }
                  },
                  fail: (res) => {
                    console.log("userInfo", userInfo)
                   app.showToastMsg("调用失败")
                  }
                })
              },
              fail: (res) => {
               app.showToastMsg("调用失败")
              }
            })
          } else {
            that.openAlbumAndCanera(type)
          }
        } else {
          console.log("res.authSetting=" + JSON.stringify(res.authSetting))
          that.openAlbumAndCanera(type)
        }
      },
      fail: (res) => {
        app.showToastMsg("调用失败")
      }
    })
  },
  openAlbumAndCanera: function (type) {
    var source='album'
    if(type==1){
      source='camera'
    }
    tt.chooseImage({
      sourceType: [source],
      count: 1,
      success: (res) => {
          console.log("AAAAAAAAA"+JSON.stringify(res))
          var imgPath=res.tempFilePaths[0]
          tt.navigateTo({
            url: '/pages/ocr/ocr?id='+this.data.id+'&imageUrl='+imgPath
          });
      },
      fail (res) {
        app.showToastMsg("调用失败")
    }
    });
  },
   onShareAppMessage(option) {
    return {
      title: '拍图取字',
      desc:'简简单单，拍个图片就能获取图片中的文字',
      path: '/pages/index/index',
    }
  },
})
